<template>
	<i-addressPicker v-model="showPicker" @confirm="confirm"></i-addressPicker>
	<!-- <i-drawer v-model="showDel" title="你确认要删除该收货地址吗？" confirm_title="确认删除" @confirm="deleteMap"></i-drawer> -->
	<view class="add_address">
		<view class="view_form">
			<!-- <i-formItem title="收货人">
				<input placeholder-class="placeholder-class" v-model="formData.userName" class="input" placeholder="请输入收货人姓名" type="text">
			</i-formItem>
			<i-formItem title="手机号">
				<input placeholder-class="placeholder-class" v-model="formData.userMobile" class="input" placeholder="请输入收货人手机号" type="text">
			</i-formItem> -->
			<i-formItem title="省市区" borderBottom="none">
				<view class="inputs" @click="showPicker = true">
					<text v-if="addressName" style="color:#16161F">{{addressName}}</text>
					<text v-else>请选择省市区</text>
					<view class="goto">
						<image src="../../static/images/goto_icon.png" mode=""></image>
					</view>
				</view>
			</i-formItem>
			<i-formItem title="详细地址" alignItems="center" borderBottom="none">
				<textarea v-model="formData.addrDetail" class="textarea" placeholder="请输入详细收货地址，精确到街道门牌信息"
					type="text"></textarea>
			</i-formItem>
			<!-- <i-formItem title="默认地址">
				<switch :checked="formData.isDefault === '0'" @change="switchChange" color="#FB652E"
					style="transform:scale(0.9)" />
			</i-formItem> -->
		</view>
		<view class="submit">
			<i-buttons padding="18rpx" :radius="4" bgColor="#FB652E" color="#fff" @click="add">保存</i-buttons>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		checkForm,
		showTost
	} from "../../utils/common";
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app'
	import {addAddress} from '../../api/index'


	interface AddRess {
		addrDetail: string
		addrId ? : string
		cityCode: string
		countyCode: string
		isDefault: '0' | '1',
		provinceCode: string,
		userMobile: string,
		userName: string,
	}

	const showDel = ref(false)
	const showPicker = ref(false)
	const addrId = ref('')
	const addressName = ref('')
	const formData = reactive < any > ({
		addrDetail: '',
		provinceCode: '',
		cityCode: '',
		countyCode: '',

	})

	onReady(() => {
		uni.setNavigationBarTitle({
			title:  "添加收货地址"
		});
	})


	const add = async () => {
		const data = checkForm(formData, ['cityCode', 'countyCode'], {
			// userMobile: {
			// 	reg: phoneReg,
			// 	message: '请输入正确手机号'
			// },
			// userName: {
			// 	reg: nameReg,
			// 	message: '请输入正确姓名'
			// },
		})
		if (data) {
			return
		}

		await addAddress({
			address: formData.addrDetail,
			cityId: formData.cityCode,
			districtId: formData.countyCode,
			provinceId: formData.provinceCode,
		})
		uni.showToast({
			icon: 'none',
			title: "添加地址成功",
			duration: 2000,
			success: () => {
				setTimeout(() => {
					uni.navigateBack()
				}, 500)
			}
		})
	}

	const confirm = (data: {
		province: string,
		city: string,
		county: string,
		address: string,
	}) => {
		addressName.value = data.address
		formData.cityCode = data.city
		formData.countyCode = data.county
		formData.provinceCode = data.province
	}

</script>

<style lang="scss" scoped>
	.add_address {
		width: 100%;
		height: 100vh;
		overflow: hidden;
		background-color: #fff;

		.view_form {
			padding: 32rpx;
			margin: 0 auto;
		}

		.input {

			width: 75%;
		}

		.inputs {
			width: 75%;
			@include dfont(28rpx);
			color: #a0a0a0;
			@include flexRS;
		}

		.submit {
			width: calc(100% - 64rpx);
			margin: 0 auto;
			margin-top: 32rpx;
		}

		.textarea {
			width: 75%;
			height: 120rpx;
		}
	}
</style>
